package forcollege.xly.com.xian.net;

import forcollege.xly.com.xian.base.BaseData;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by xuleyuan on 2018/8/15
 */

public class Request<R extends BaseData> {

    private String url;//接口地址

    private Class<R> mClass;

    private HashMap<String, Object> params;

    private int method;

    public static final int POST = 1;

    public static final int GET = 2;

    public Request() {

    }

    public Request<R> setMethod(int method) {
        this.method = method;
        return this;
    }

    public Request<R> setParams(HashMap<String, Object> params) {
        this.params = params;
        return this;
    }

    public Request<R> setmClass(Class<R> mClass) {
        this.mClass = mClass;
        return this;
    }

    public Request<R> setUrl(String url) {
        this.url = url;
        return this;
    }

    public Observable<R> doRequest() {
        Observable<R> result;
        switch (method) {
            case POST:
                result = RetrofitClient.getRetrofit()
                        .create(RetrofitApi.class)
                        .post(url, params)
                        .compose(JsonTransformer.transformer(mClass))
                        .subscribeOn(Schedulers.io());

                return result;
            case GET:
                result = RetrofitClient.getRetrofit()
                        .create(RetrofitApi.class)
                        .get(url, params)
                        .compose(JsonTransformer.transformer(mClass))
                        .subscribeOn(Schedulers.io());
                return result;
        }
        return null;
    }

}
